


/**
 * primitive Sphere 大量加载
 * 
 */
import Primitive from "../Primitive"

const DEFAULT_STYLE = {

}

export default class SpherePrimitive extends Primitive {
  /**
   * 
   * @param {*} positions 
   * @param {*} radius 
   * @param {*} options 
   */
  constructor(positions, radius, options = {}) {
    super()
    this._positions = positions
    this._radius = radius
    this._type = 'sphere_primitive'
    this._style = {
      ...DEFAULT_STYLE,
      ...options
    }

    this.delegate = new Cesium.Primitive({
      geometryInstances: new Cesium.GeometryInstance({
        geometry: this.createGeometry()
      }),
      modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame(
        this._positions,
        new Cesium.HeadingPitchRoll(
          Cesium.Math.toRadians(this._heading),
          Cesium.Math.toRadians(this._pitch),
          Cesium.Math.toRadians(this._roll)
        )
      ),
      asynchronous: false
    })

    this.mounted()
  }

  /**
   * 初始化
   */

  mounted() {
    this.positions = this._positions

    !this.delegate.appearance && this.setAppearance({ type: 'Color' })
  }
  /**
   * 创建图形
   * @returns 
   */
  createGeometry() {

    return new Cesium.SphereGeometry({
      radius: this._radius
    })
  }

  /**
   * 设置geometry样式
   * @param {*} style 
   * @returns 
   */

  setStyle(style) {
    if (!style) return
    Object.assign(this._style, { ...style })
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }

  get positions() {
    return this._positions
  }

  set positions(val) {
    this._positions = val
    this.setPosition()
  }

  get radius() {
    return this._radius
  }

  set radius(radius) {
    this._radius = radius
    this.setRadius(radius)
  }

  setRadius(radius) {
    this._radius = radius
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }
}
